//
//  DocumentsCacheOperationOutput.m
//  Documents
//
//  Created by Alex Di Mango 
//  Copyright (c) 2012 Whardoo.com -  All rights reserved.
//

#import <fcntl.h>
#import "DocumentsCache.h"
#import "MyDocument.h"
#import "DocumentsConstants.h"
#import "AppDelegate.h"

@implementation DocumentsCacheOperationOutput
@synthesize operationOK, needsDownload, operationMessage, filePath;
-(void)dealloc
{
    self.operationMessage = nil;
    self.filePath = nil;

    [super dealloc];
}
@end

@implementation DocumentsCache

+(BOOL)isDocFolderFull:(NSString*)path forMaxDimension:(int)dim
{
    int fileSize = (([DocumentsCache docFolderSize:path] / 1024) / 1024);
    
    return fileSize > dim;
}

+(int)docFolderSize:(NSString *)path
{
    NSString  *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
    NSString  *docFolderPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, path];
    NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:docFolderPath error:nil];
    NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
    
    NSString *fileName;
    int fileSize = 0;
    NSError *error = nil;

    while (fileName = [filesEnumerator nextObject]) {
        
        NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[docFolderPath stringByAppendingPathComponent:fileName] error:&error];
        fileSize += [fileDictionary fileSize];
    }
    
    return fileSize;
}

+(float)docFolderFreeSpace:(NSString *)path forMaxDimension:(int)dim
{
    float fileSize = [DocumentsCache docFolderSize:path];
    fileSize = ((fileSize / 1024) / 1024);
    return fileSize;
}

+(void)removeFromCache:(MyDocument *)document
{
    [[NSFileManager defaultManager] removeItemAtPath:[document localDocPath] error:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FREE_SPACE object:self];
}

+(void)emptyCache
{
    NSError *error = nil;
    
    for (MyDocument *md in [MyDocument myDocuments])
    {
        [[NSFileManager defaultManager] removeItemAtPath:[md localDocPath] error:&error];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_FREE_SPACE object:self];

}

+ (BOOL)isValidPath:(NSString *)filePath
{
	BOOL state = NO;
    
	if (filePath != nil) // Must have a file path
	{
		const char *path = [filePath fileSystemRepresentation];
        
		int fd = open(path, O_RDONLY); // Open the file
        
		if (fd > 0) // We have a valid file descriptor
		{
			close(fd); // Close the file
            state = YES;
		}
	}
    
	return state;
}

//Cleans myDocuments folder
+(void)documentsFolderCleaning{



}


+(NSString*)localFilePathFromDoc:(id)document
{
    NSString *localPath = nil;
    
    if ([document isKindOfClass:[MyDocument class]])
    {   
        MyDocument *myDoc = (MyDocument*)document;
        localPath = [myDoc localDocPath];
    }    
    return localPath;
}

+(BOOL)docExists:(id)document
{
    return [DocumentsCache isValidPath:[DocumentsCache localFilePathFromDoc:document]];
}


@end
